package cn.edu.scau.dormitory_management_system.service.impl;


import cn.edu.scau.dormitory_management_system.entity.PermissionEntity;
import cn.edu.scau.dormitory_management_system.entity.dto.PermissionDto;
import cn.edu.scau.dormitory_management_system.service.PermissionService;
import cn.edu.scau.dormitory_management_system.dao.PermissionDao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionDao,PermissionEntity> implements PermissionService {
    @Autowired(required = false)
    PermissionDao permissionDao;
    @Override
    public List<PermissionEntity> selectPermission(int userId){
        return permissionDao.selectPermission(userId);
    }

    @Override
    public List<PermissionDto> listAll(int roleId) {
        QueryWrapper<PermissionEntity> query = new QueryWrapper<>();
        query.orderByAsc("menu_id")
        .ne("is_home","1");
        List<PermissionEntity> list = list(query);
        ArrayList<PermissionDto> permissionDtos = new ArrayList<>();
        List<Integer> integers = permissionDao.listAll(roleId);
        for (int i = 0; i < integers.size(); i++) {
            System.out.println(integers.get(i));
        }
        int j = 0;
        for (int i = 0; i < list.size(); i++) {
            PermissionDto permissionDto = new PermissionDto();
            permissionDto.setPermissionEntity(list.get(i));
            if( j != integers.size()){
                if(integers.get(j).equals(list.get(i).getMenuId())){
                    permissionDto.setFlag(true);
                    j++;
                }
            }
            permissionDtos.add(permissionDto);
        }
        return permissionDtos;
    }

    @Override
    public Boolean isTrue(PermissionEntity permissionEntity) {
        if(permissionEntity.getType() == 1){
            if(permissionEntity.getUrl() == null){
                return false;
            }
        }
        return true;
    }
}
